﻿using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Zvezda.FSO2.DB;

namespace FSO
{
    public partial class UserControlSeachCostProtocol : UserControl, InterfaceSearchControl
    {

        private bool _openOnDblClick = false;

        private bool _isNotFill = true;

        public UserControlSeachCostProtocol()
        {
            InitializeComponent();

            radGridViewList.MultiSelect = true;
        }

        #region InterfaceSearchControl Members
        public bool OpenOnDoubleClick
        {
            get
            {
                return _openOnDblClick;
            }
            set
            {
                _openOnDblClick = value;
            }
        }

        public bool EnableFiltering
        {
            get
            {
                return radGridViewList.EnableFiltering;
            }
            set
            {
                radGridViewList.EnableFiltering = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                DataSetFSO.VIEW_SEARCH_COST_PROTOCOLRow dr;
                DataRow returnRow;
                if (radGridViewList.SelectedRows.Count > 0)
                {
                    dr = (DataSetFSO.VIEW_SEARCH_COST_PROTOCOLRow)(((DataRowView)(radGridViewList.SelectedRows.First()).DataBoundItem)).Row;
                    returnRow = cosT_PROTOCOLTableAdapter1.GetDataByID(dr.ID).FindByID(dr.ID);
                }
                else
                {
                    returnRow = null;
                }
                return returnRow;
            }
        }

        public DataSet OwnerDataSet
        {
            get {return dataSetFSO1; }
            set { _isNotFill = false; dataSetFSO1 = (DataSetFSO)value; InitializeGridView(); }
        }

        public GridViewSelectedRowsCollection SelectedRows
        {
            get
            {
                return radGridViewList.SelectedRows;
            }
        }

        public void RefreshList()
        {
            vieW_SEARCH_COST_PROTOCOLTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL);
        }

        #endregion

        private void UserControlSeachCostProtocol_Load(object sender, EventArgs e)
        {
            if (_isNotFill)
            {
                vieW_SEARCH_COST_PROTOCOLTableAdapter1.Fill(dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL);
            }
            InitializeGridView();
        }

        private void InitializeGridView()
        {
            GridViewTextBoxColumn colNumber = new GridViewTextBoxColumn("NUMBER", dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL.NUMBERColumn.ColumnName);
            colNumber.HeaderText = "№";

            GridViewTextBoxColumn colContract = new GridViewTextBoxColumn("CONTRACT", dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL.CONTRACT_NUMBERColumn.ColumnName);
            colContract.HeaderText = "Договор";

            GridViewTextBoxColumn colOrg = new GridViewTextBoxColumn("ORGANIZATION", dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL.ORG_NAMEColumn.ColumnName);
            colOrg.HeaderText = "Организация";

            GridViewDateTimeColumn colSDate = new GridViewDateTimeColumn("START_DATE", dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL.START_DATEColumn.ColumnName);
            colSDate.HeaderText = "С";
            colSDate.FormatString = "{0:d}";

            GridViewDateTimeColumn colEDate = new GridViewDateTimeColumn("END_DATE", dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL.END_DATEColumn.ColumnName);
            colEDate.HeaderText = "По";
            colEDate.FormatString = "{0:d}";

            GridViewCheckBoxColumn colAgree = new GridViewCheckBoxColumn("IS_AGREE", dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL.IS_AGREEDColumn.ColumnName);
            colAgree.HeaderText = "Согласован";

            GridViewTextBoxColumn colGroups = new GridViewTextBoxColumn("PROD_GROUPS", dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL.PROD_GROUPSColumn.ColumnName);
            colGroups.HeaderText = "Группы товаров";

            radGridViewList.MasterTemplate.Columns.Clear();       
            radGridViewList.MasterTemplate.Columns.Add(colNumber);
            radGridViewList.MasterTemplate.Columns.Add(colContract);
            radGridViewList.MasterTemplate.Columns.Add(colOrg);
            radGridViewList.MasterTemplate.Columns.Add(colSDate);
            radGridViewList.MasterTemplate.Columns.Add(colEDate);
            radGridViewList.MasterTemplate.Columns.Add(colAgree);
            radGridViewList.MasterTemplate.Columns.Add(colGroups);
            radGridViewList.DataSource = dataSetFSO1.VIEW_SEARCH_COST_PROTOCOL;

            //radGridViewList.MultiSelect = true;
        }

        private void radGridViewList_CellDoubleClick(object sender, GridViewCellEventArgs e)
        {
            DataRow selectedRow = this.SelectedRow;

            if (selectedRow != null && _openOnDblClick)
            {

                FormCostProtocol formInvIn = new FormCostProtocol((DataSetFSO.COST_PROTOCOLRow)selectedRow);
                if (!FormHelper.HasOpenedForm(formInvIn.FormName))
                {
                    formInvIn.MdiParent = FormHelper.MainForm;
                    formInvIn.Show();
                    formInvIn.Select();
                }

            }
        }
    }
}
